package com.base.common;

import java.io.IOException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class CryptionData {
	private static String EncryptionString;

	  private static byte[] EncryptionByteData(byte[] SourceData)
	    throws Exception
	  {
	    byte[] retByte = (byte[])null;

	    byte[] EncryptionByte = getByte();
	    DESKeySpec dks = new DESKeySpec(EncryptionByte);
	    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
	    SecretKey securekey = keyFactory.generateSecret(dks);

	    SecureRandom sr = new SecureRandom();

	    Cipher cipher = Cipher.getInstance("DES");

	    cipher.init(1, securekey, sr);

	    retByte = cipher.doFinal(SourceData);
	    return retByte;
	  }

	  private static byte[] getByte()
	  {
	    BASE64Decoder bd = new BASE64Decoder();
	    byte[] sorData = (byte[])null;
	    try {
	      return EncryptionString.getBytes("UTF-8");
	    }
	    catch (IOException e)
	    {
	      e.printStackTrace();
	    }return null;
	  }

	  private static byte[] DecryptionByteData(byte[] SourceData)
	    throws Exception
	  {
	    byte[] retByte = (byte[])null;

	    byte[] EncryptionByte = (byte[])null;

	    EncryptionByte = getByte();
	    DESKeySpec dks = new DESKeySpec(EncryptionByte);
	    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
	    SecretKey securekey = keyFactory.generateSecret(dks);

	    SecureRandom sr = new SecureRandom();

	    Cipher cipher = Cipher.getInstance("DES");

	    cipher.init(2, securekey, sr);

	    retByte = cipher.doFinal(SourceData);

	    return retByte;
	  }

	  public static String encryptionStringData(String sourceData)
	  {
	    if (sourceData == null) {
	      return "";
	    }
	    String retStr = null;
	    byte[] retByte = (byte[])null;
	    try
	    {
	      byte[] sorData = sourceData.getBytes();

	      byte[] EncryptionByte = (byte[])null;

	      EncryptionByte = getByte();

	      retByte = DesUtil.encrypt(EncryptionByte, sourceData);

	      BASE64Encoder be = new BASE64Encoder();
	      retStr = be.encode(retByte);
	    } catch (Exception e) {
	      e.printStackTrace();
	      return null;
	    }

	    return retStr;
	  }

	  public static String decryptionStringData(String sourceData)
	  {
	    if (sourceData == null) {
	      return "";
	    }
	    String retStr = null;
	    byte[] retByte = (byte[])null;
	    try
	    {
	      BASE64Decoder bd = new BASE64Decoder();
	      byte[] sorData = bd.decodeBuffer(sourceData);

	      byte[] EncryptionByte = (byte[])null;

	      EncryptionByte = getByte();

	      retStr = DesUtil.decrypt(EncryptionByte, sorData);
	    }
	    catch (Exception e)
	    {
	      e.printStackTrace();
	      return null;
	    }
	    return retStr;
	  }

	  public static String getEncryptionString()
	  {
	    return EncryptionString;
	  }

	  public static void setEncryptionString(String encryptionString) {
	    EncryptionString = encryptionString;
	  }

	  public static void main(String[] args) {
	    setEncryptionString("yz6p2nr42=");
	    String sourceData = "2010-01-19 17:55abcdeffedcba";

	    String out = encryptionStringData(sourceData);
	    System.out.println(out);
	    String in = decryptionStringData(out);
	    System.out.println(in);
	  }
}
